package com.iyue.activity.helper;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;

import com.iyue.Config;
import com.iyue.SpringHelper;
import com.iyue.activity.pojo.ActivityCategory;
import com.iyue.activity.pojo.ActivityInfo;
import com.iyue.activity.service.ActivityBiz;
import com.iyue.activity.service.CategoryBiz;
import com.iyue.framework.cache.IMemcachedCache;
import com.iyue.servlet.taglibs.FunctionsUtil;

/**
 * 《网站入口》控制器
 * 
 * @author 孙晓晔
 * @version $Id: PortalAction.java,v 1.1 2010/10/14 17:07:15 fresh Exp $
 */
public class ActivityFunctions extends FunctionsUtil {

	private static final String MACTIVITY_VIEW = "com.iyue.activity.pojo.views_";
	
	
	public static List<ActivityCategory> getCategories(Integer level) {
		if (level == null) {
			return null;
		}
		
		CategoryBiz biz = (CategoryBiz) SpringHelper.getBean("CategoryBiz");
		
		return biz.getCategoriesByLevel(level);

	}

	/**
	 * 获取活动适用区域
	 * 
	 * @param aid
	 * @return
	 */
	public static String area(Integer aid) {
		if (aid == null) {
			return "";
		}

		ActivityBiz biz = (ActivityBiz) SpringHelper.getBean("ActivityBiz");

		return biz.getAreaById(aid);
	}

	/**
	 * 解析图片URL地址
	 * 
	 * @param url
	 * @return
	 */
	public static String pic(String url) {
		if (StringUtils.isBlank(url)) {
			return "";
		}

		String[] ss = url.split("\\,");
		if (ss == null || ss.length <= 0) {
			return null;
		}

		Config config = (Config) SpringHelper.getBean("test");
		String ret = config.getFile() + "/getfile/" + url;

		return ret;
	}

	/**
	 * 识别活动分类（移动，联通，银行，宽带，网站）
	 * @param cid
	 * @return
	 */
	public static String type(Integer cid) {
		if (cid == null) {
			return "layout";
		}

		String ret = ActivityCategory.MAP_CATEGORY.get(cid);
		if (ret == null) {
			return "layout";
		}

		return ret;
	}

	/**
	 * 分类信息
	 * 
	 * @param category
	 * @return
	 */
	public static ActivityCategory category(Integer category) {
		if (category == null) {
			return null;
		}

		CategoryBiz biz = (CategoryBiz) SpringHelper.getBean("CategoryBiz");

		ActivityCategory ret = biz.get(category);

		return ret;
	}

	/**
	 * 获取活动信息
	 * 
	 * @param aid
	 * @return
	 */
	public static ActivityInfo activity(Integer aid) {
		if (aid == null) {
			return null;
		}

		ActivityBiz biz = (ActivityBiz) SpringHelper.getBean("ActivityBiz");

		ActivityInfo ret = biz.getActivityInfo(aid);

		return ret;
	}

	/**
	 * 判断是否过去
	 * 
	 * @param expiretime
	 * @return
	 */
	public static Integer isExpire(Date expiretime) {
		if (expiretime == null) {
			return 0;
		}

		if (expiretime.before(new Date())) {
			return 1;
		}

		return 0;
	}

	/**
	 * 获取该分类的子类
	 * 
	 * @param cid
	 * @return
	 */
	public static List<ActivityCategory> subs(Integer cid) {
		if (cid == null) {
			return null;
		}

		CategoryBiz biz = (CategoryBiz) SpringHelper.getBean("CategoryBiz");

		return biz.getCategories(cid);
	}

	/**
	 * 获取该分类所有的父分类
	 * 
	 * @param cid
	 * @return
	 */
	public static List<Integer> pids(Integer cid) {
		if (cid == null) {
			return null;
		}

		CategoryBiz biz = (CategoryBiz) SpringHelper.getBean("CategoryBiz");
		List<Integer> list = biz.getPidById(cid);
		if (list == null || list.isEmpty()) {
			return null;
		}

		List<Integer> ret = new ArrayList<Integer>();
		int size = list.size();
		for (int i = 0; i < size; i++) {
			ret.add(i, list.get(size - i - 1));
		}

		return ret;
	}

	/**
	 * 相片浏览数
	 * 
	 * @param username
	 * @param aid
	 * @param views
	 * @param i
	 * @return
	 */
	public static Integer views(Integer aid, Integer views, Integer i) {
		if (aid == null) {
			return 1;
		}

		IMemcachedCache cache = (IMemcachedCache) SpringHelper
				.getBean("ActivityIMemcachedCache");
		String key = ActivityFunctions.MACTIVITY_VIEW + aid;
		Object obj = cache.get(key);
		if (obj == null) {
			cache.add(key, 1);

			return views + i;
		}

		if (!(obj instanceof Integer)) {
			cache.delete(key);
			cache.add(key, 1);

			return views + i;
		}

		Integer a = (Integer) obj;
		if (a < 10) {
			cache.add(key, a + i);

			return views + a + i;
		}

		Integer ret = views + a + i;
		ActivityBiz biz = (ActivityBiz) SpringHelper.getBean("ActivityBiz");
		biz.update_activity_views(aid, ret);
		cache.delete(key);

		return ret;
	}

}
